# -*- coding: utf-8 -*-
import logging
# from retrying import retry
# from pysnmp import debug
import os
#  (sendNotification,
#                           SnmpEngine,
#                           UdpTransportTarget,
#                           ObjectType,
#                           ContextData,
#                           CommunityData,
#                           ObjectIdentity,
#                           OctetString)
logger = logging.getLogger(__name__)


def retry_if_io_error(exception):
    """Return True if we should retry (in this case when it's an IOError), False otherwise"""
    # debug.logger.Debug("retry")
    logger.warning("retry")
    return isinstance(exception, IOError)


# @retry(retry_on_exception=retry_if_io_error, stop_max_attempt_number=7, wait_fixed=500, wrap_exception=False)
def send_msg(host, port, ip, action, tm, files):
    cmd = ('snmptrap -v 2c -c public {}:{} "" 1.3.6.1.4.1.171.10.37.40 ' +
           '1.3.6.1.4.1.171.10.371.1 s "{}" ' +
           '1.3.6.1.4.1.171.10.371.2 s "{}" ' +
           '1.3.6.1.4.1.171.10.371.3 s "{}" ' +
           '1.3.6.1.4.1.171.10.371.4 s "{}"'
           ).format(host, port, ip, action, tm, ",".join(files))
    print(cmd)
    res = os.popen(cmd)
    output = res.read()
    if output:
        raise IOError


def send_listening_msg(host, port):
    cmd = ('snmptrap -v 2c -c public {}:{} "" 1.3.6.1.4.1.171.10.37.40 ' +
           '1.3.6.1.4.1.171.10.371.5 s "{}" '
           ).format(host, port, "Listening")
    print(cmd)
    res = os.popen(cmd)
    output = res.read()
    if output:
        raise IOError


if __name__ == "__main__":
    send_msg("127.0.0.1", 161, "ip", "action", "20211026xx", ["1", "2", "3", "4"])
